package com.watch.system.controller;

import com.watch.system.common.model.Option;
import com.watch.system.common.result.Result;
import com.watch.system.model.form.BrandForm;
import com.watch.system.model.query.MenuQuery;
import com.watch.system.model.vo.*;
import com.watch.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.watch.system.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 *资源
 * @author gmli
 * @since 2024/1/29
 */
@Tag(name = "12.品牌")
@RestController
@RequestMapping("/api/car/brand")
@RequiredArgsConstructor
public class BrandListController {

    private final BrandService brandService;

    @Operation(summary = "菜单列表")
    @GetMapping
    public Result<List<BrandVO>> listMenus(@ParameterObject MenuQuery queryParams) {
        List<BrandVO> menuList = brandService.listMenus(queryParams);
        AtomicInteger idex= new AtomicInteger();

        menuList.forEach(item->{
            item.setIsLastLevel(1);
            idex.set(idex.get() + 1);
            item.setInDx(idex.get());
            AtomicInteger subIdx= new AtomicInteger();
            item.getChildren().forEach(subItem->{
                subIdx.set(subIdx.get() + 1);
                subItem.setInDx(subIdx.get());
                subItem.setIsLastLevel(3);
                AtomicInteger lastIdx= new AtomicInteger();
                subItem.getChildren().forEach(lastItem->{
                    lastItem.setIsLastLevel(0);
                    lastIdx.set(lastIdx.get() + 1);
                    lastItem.setInDx(lastIdx.get());
                });
            });
        });
        return Result.success(menuList);
    }

    @Operation(summary = "查询品牌列表")
    @GetMapping("/brandList")
    public Result<List<BrandVO>> brandList() {
        MenuQuery queryParams=new MenuQuery();
        List<BrandVO> brandVOList=new ArrayList<>();
        List<BrandVO> menuList = brandService.listMenus(queryParams);
        menuList.forEach(meun -> {
            if(CollectionUtils.isNotEmpty(meun.getChildren())){
                meun.getChildren().forEach(subBrand->{
                    brandVOList.add(subBrand);
                });
            }
        });
        return Result.success(brandVOList);
    }


    @Operation(summary = "菜单下拉列表")
    @GetMapping("/options")
    public Result listMenuOptions() {
        List<Option> menus = brandService.listMenuOptions();
        return Result.success(menus);
    }

    @Operation(summary = "路由列表")
    @GetMapping("/routes")
    public Result<List<RouteVO>> listRoutes() {
        List<RouteVO> routeList = brandService.listRoutes();
        return Result.success(routeList);
    }

    @Operation(summary = "菜单表单数据")
    @GetMapping("/{id}/form")
    public Result<BrandForm> getBrandForm(@Parameter(description =  "菜单ID") @PathVariable Long id) {
        BrandForm menu = brandService.getMenuForm(id);
        return Result.success(menu);
    }

    @Operation(summary = "新增菜单")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:menu:add')")
    @PreventDuplicateSubmit
    public Result addMenu(@RequestBody BrandForm brandForm) {
        boolean result = brandService.saveMenu(brandForm);
        return Result.judge(result);
    }

    @Operation(summary = "修改菜单")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('sys:menu:edit')")
    public Result updateMenu(@RequestBody BrandForm menuForm) {
        boolean result = brandService.saveMenu(menuForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除菜单")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('sys:menu:delete')")
    public Result deleteMenu(@Parameter(description ="菜单ID，多个以英文(,)分割")@PathVariable("id") Long id) {
        boolean result = brandService.deleteMenu(id);
        return Result.judge(result);
    }

    @Operation(summary = "修改菜单显示状态")
    @PatchMapping("/{menuId}")
    public Result updateMenuVisible(@Parameter(description =  "菜单ID") @PathVariable Long menuId, @Parameter(description =  "显示状态(1:显示;0:隐藏)") Integer visible) {
        boolean result =brandService.updateMenuVisible(menuId, visible);
        return Result.judge(result);
    }

}
